package com.liuche.pictureserver.common.resp;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.liuche.pictureserver.aop.annotation.MaskedField;
import lombok.AccessLevel;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 分页响应
 *
 * @author liuche
 */
@Data
@Accessors(chain = true)
@RequiredArgsConstructor(staticName = "of", access = AccessLevel.PRIVATE)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PageData<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = -4257092979237049883L;

    /**
     * 结果集
     */
    @MaskedField
    private List<T> list;
    /**
     * 总记录数
     */
    private Long totalCount;
    /**
     * 页码，从1开始
     */
    private Long pageNo;
    /**
     * 每页显示数量
     */
    private Integer pageSize;
    /**
     * 扩展信息
     */
    private Map<String, Object> extra;

    /**
     * 正常返回
     *
     * @param data 数据内容
     * @param <T>  返回数据的类型
     * @return 包装后的返回
     */
    public static <T> PageData<T> create(List<T> data) {
        return create(data, null);
    }

    public static <T> PageData<T> create(List<T> data, Long totalCount) {
        return create(data, totalCount, null, null);
    }

    public static <T> PageData<T> create(List<T> data, Long totalCount, Long pageNo, Integer pageSize) {
        return create(data, totalCount, pageNo, pageSize, null);
    }

    public static <T> PageData<T> create(List<T> data, Long totalCount, Long pageNo, Integer pageSize,
                                         Map extra) {
        return PageData.<T>of().setList(data).setTotalCount(totalCount).setPageNo(pageNo).setPageSize(pageSize)
                .setExtra(extra);
    }
}